package ms.jacrim.framework;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import ms.jacrim.pddl.PddlSolution;
import ms.spm.IAppContext;
import ms.utils.XmlFile;

public class Model {

	public final PddlModel PddlModel;
	public final SolutionGenerator Generator;
	public final SolutionManager SolutionManager;
	public final IAppContext Context;
	public final List<Event> Events;
	
	public final Logger log = Logger.getLogger("Model");

	public Model(IAppContext context, XmlFile modelFile) throws Exception {
		Context = context;
		
		PddlModel = new PddlModel(modelFile);
		Generator = new SolutionGenerator(context, this);
		SolutionManager = new SolutionManager(context, this);
		Events = new ArrayList<Event>();
	}

	public static Model createModel() throws Exception {
		IAppContext context = new ms.spm.implement.SimpleAppContext(null, "$root");

		context.addService(new MessageOutput() {

			@Override
			public void write(String category, String message) {
				System.out.print(message);
			}

			@Override
			public void write(String category, String format, Object... args) {
				System.out.print(String.format(format, args));
			}
		});
		
		XmlConfiguration config;
		
		context.addService(config = new XmlConfiguration(new XmlFile("conf/jacrim.config.xml")));

		Model model = new Model(context, new XmlFile((String) config.get("model_file")));
		
		return model;
	}
	
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// test purpose
		IAppContext context = new ms.spm.implement.SimpleAppContext(null, "$root");
		
		context.addService(new MessageOutput() {

			@Override
			public void write(String category, String message) {
				System.out.print(message);
			}

			@Override
			public void write(String category, String format, Object... args) {
				System.out.print(String.format(format, args));
			}
		});
		
		XmlConfiguration config;
		context.addService(config = new XmlConfiguration(new XmlFile("conf/jacrim.config.xml")));
		
		Model model = new Model(context, new XmlFile((String) config.get("model_file")));
		
		List<PddlSolution> sols = model.SolutionManager.collectSolutions(true);
		
		System.out.printf("There are %d solution(s) found.\n", sols.size());
		
		System.exit(0);
	}

}
